package org.fall.mvc.handler;

import com.atguigu.crowd.constant.CrowdConstant;
import com.github.pagehelper.PageInfo;
import com.atguigu.crowd.entity.Admin;
import org.fall.service.api.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;

@Controller
public class AdminHandler {

    @Autowired
    private AdminService adminService;

    @RequestMapping("/admin/page/doUpdate.html")
    public String updateAdmin(
            Admin admin,
            @RequestParam("pageNum") Integer pageNum,
            @RequestParam("keyword") String keyword
    ) {
        adminService.update(admin);

        return "redirect:/admin/page/page.html?pageNum=" + pageNum + "&keyword" + keyword;
    }


    @RequestMapping("/admin/page/update/{adminId}/{pageNum}/{keyword}.html")
    public String toUpdatePage(
            @PathVariable("adminId") Integer adminId,
            @PathVariable("pageNum") Integer pageNum,
            @PathVariable("keyword") String keyword,
            ModelMap modelMap) {
        // 1、根据adminId查询Admin对象
        Admin admin = adminService.getAdminById(adminId);

        // 2、将admin对象存入模型
        modelMap.addAttribute("admin", admin);
        modelMap.addAttribute("pageNum", pageNum);
        modelMap.addAttribute("keyword", keyword);

        return "admin-update";
    }

    @PreAuthorize("hasAnyAuthority('user:save')")
    @RequestMapping("/admin/page/doSave.html")
    public String save(Admin admin) {
        adminService.saveAdmin(admin);

        return "redirect:/admin/page/page.html?pageNum=" + Integer.MAX_VALUE;
    }


    @RequestMapping("/admin/page/remove/{adminId}/{pageNum}/{keyword}")
    public String remove(
            @PathVariable("adminId") Integer adminId,
            @PathVariable("pageNum") Integer pageNum,
            @PathVariable("keyword") String keyword
    ) {

        adminService.remove(adminId);

        //页面跳转
        return "redirect:/admin/page/page.html?pageNum=" + pageNum + "&keyword=" + keyword;
    }


    //显示admin的数据
    @RequestMapping("/admin/page/page.html")
    public String getAdminPage(
            // 传入的关键字，若未传入，默认值为一个空字符串（不能是null）
            @RequestParam(value = "keyword", defaultValue = "") String keyword,
            // 传入的页码，默认值为1
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            // 传入的页面大小，默认值为5
            @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
            // ModelMap用于给前端带数据
            ModelMap modelMap) {

        //从AdminService中得到对应传参的列表
        PageInfo<Admin> pageInfo = adminService.getPageInfo(keyword, pageNum, pageSize);

        //将得到的PageInfo存入modelMap，传给前端
        modelMap.addAttribute(CrowdConstant.ATTR_NAME_PAGE_INFO, pageInfo);

        //进入对应的显示管理员信息的页面（/WEB-INF/admin-page.jsp）
        return "admin-page";
    }


    @RequestMapping("/admin/do/logout.html")
    public String doLogout(HttpSession session) {
        //强制session失效
        session.invalidate();
        return "redirect:/admin/to/login/page.html";
    }


    @RequestMapping("admin/login/doLogin.html")
    public String doLogin(
            @RequestParam("login-user") String username,
            @RequestParam("login-pwd") String password,
            HttpSession session
    ) {

        //通过service层方法得到Admin对象
        Admin admin = adminService.getAdminByUsername(username, password);

        //将admin对象放入session域中
        session.setAttribute(CrowdConstant.LOGIN_ADMIN_NAME, admin);

        //重定向到登录完成后的主页面（重定向防止重复提交表单，增加不必要的数据库访问）
        return "redirect:/admin/to/main/page.html";

    }

}
